﻿#include "../MeasureLineWidthSA.h"
#include "../UnitConvert.h"
#include "../MeasureDataSet.h"
#include <QDebug>
#include <QtMath>

namespace Qly {

MeasureLineWidthSA::MeasureLineWidthSA(MeasureDataSet *parent)
    :MeasureElement(parent)
{
    m_cos_theta  = 1.0;
    m_sin_theta = 0.0;
    m_measureType = LineWidth_SA;

    m_results_pix.resize(3);
    m_results_um.resize(3);// 输出有线宽、cos 、sin 三个参数

    m_searchWidth = parent->m_lineWidthSA_width;
    m_results_pix[0] = m_searchWidth;
    m_results_um[0] = m_searchWidth * parent->m_pixSize_um;

    qDebug() << "MeasureLineWidthSA::MeasureLineWidthSA(MeasureDataSet *parent)  m_searchWidth = " << m_searchWidth;
    //m_keyPoints 0-1 存储的是中心线
    //m_keyPoints 2-3 是第一条平行线
    //m_keyPoints 4-5 是第二条平行线
    m_keyPoints_um.resize(6);
    m_keyPoints_pix.resize(6);
    m_textTemplate = parent->m_textTemplate_MeasureParalleLine;
}

void MeasureLineWidthSA::adjust(QPointF start_pix, QPointF end_pix, double width_pix)
{
    double ps = m_pParent->m_pixSize_um;
    m_keyPoints_pix[0] = start_pix;
    m_keyPoints_pix[1] = end_pix;

    m_keyPoints_um[0] = m_keyPoints_pix[0] * ps;
    m_keyPoints_um[1] = m_keyPoints_pix[1] * ps;

    m_results_pix[0] = width_pix;
    m_results_um[0] = width_pix * ps;

    compute();
    updateText(true);


}

void MeasureLineWidthSA::computeLengthSlope()
{
    m_length_pix = hypot( m_keyPoints_pix[0].x() - m_keyPoints_pix[1].x(), m_keyPoints_pix[0].y() - m_keyPoints_pix[1].y() );
    m_length_um = hypot( m_keyPoints_um[0].x() - m_keyPoints_um[1].x(), m_keyPoints_um[0].y() - m_keyPoints_um[1].y() );
    if( m_keyPoints_pix[0].x() == m_keyPoints_pix[1].x() ) // 也包括了  m_length == 0 的情况，下面就不需要特殊处理了
    {
        m_cos_theta = 0.0;
        m_sin_theta  = 1.0;
    }
    else
    {
        m_cos_theta = ( m_keyPoints_pix[1].x() - m_keyPoints_pix[0].x() ) / m_length_pix;
        m_sin_theta = ( m_keyPoints_pix[1].y() - m_keyPoints_pix[0].y() ) / m_length_pix;
    }
    m_results_pix[1]= m_cos_theta;
    m_results_um[1] = m_cos_theta;

    m_results_pix[2]= m_sin_theta;
    m_results_um[2] = m_sin_theta;
}

int MeasureLineWidthSA::mouseLButtonPress(QPointF point_pix, QPointF point_um)
{
    if( m_measureState == DS_IDLE )
    {
        m_keyPoints_pix[0] = point_pix;
        m_keyPoints_um[0] = point_um;
        m_keyPoints_pix[1] = point_pix;
        m_keyPoints_um[1] = point_um;
        m_measureState = DS_STATE1;
    }
    else if(m_measureState == DS_STATE1)
    {
        m_keyPoints_pix[1] = point_pix;
        m_keyPoints_um[1] = point_um;
        computeLengthSlope();
        compute();
        updateText(true);
        m_measureState = DS_FINISH;
    }
    return m_measureState;
}

int MeasureLineWidthSA::mouseMove(QPointF point_pix, QPointF point_um)
{
    if( m_measureState == DS_STATE1 )
    {
        m_keyPoints_pix[1] = point_pix;
        m_keyPoints_um[1] = point_um;
        compute();
        return 1;
    }
    return 0;
}

void MeasureLineWidthSA::updateText(bool updatePosition)
{
    QString text = m_textTemplate;
//    qDebug() << m_results_um[0];
    m_pParent->m_unitConvert_MeasureLineWidthSA.replaceLength(text, m_results_um[0], "@length", "@unit");

    QString valueStr = QString::number(m_id);
    text.replace("@id", valueStr, Qt::CaseSensitive);

    m_textBlock.setText(text);

    if(updatePosition)
    {
        QPointF p_pix = (m_keyPoints_pix[0] + m_keyPoints_pix[1]) / 2 ;
        QPointF p_um = (m_keyPoints_um[0] + m_keyPoints_um[1]) / 2;
        m_textBlock.moveTo(p_pix, p_um);
    }
}

void MeasureLineWidthSA::paint(QPainter &painter)
{
    if( m_measureState == DS_STATE1)
    {
        painter.save();
        painter.setPen(m_pen);
        painter.drawLine ( m_keyPoints_pix[0], m_keyPoints_pix[1] );
        painter.drawLine ( m_keyPoints_pix[2], m_keyPoints_pix[3] );
        painter.drawLine ( m_keyPoints_pix[4], m_keyPoints_pix[5] );
        painter.restore();
    }
    else if(m_measureState == DS_FINISH)
    {
        painter.save();
        painter.setPen(m_pen);
        painter.drawLine ( m_keyPoints_pix[2], m_keyPoints_pix[3] );
        painter.drawLine ( m_keyPoints_pix[4], m_keyPoints_pix[5] );
        m_textBlock.draw(painter);

        if(m_textMoving)
        {
            QPen pen = m_pen;
            pen.setStyle(Qt::DashDotLine);
            painter.setPen(pen);
            QPointF p = m_textBlock.anchorPoint();
            painter.drawLine(p, (m_keyPoints_pix[0] + m_keyPoints_pix[1]) / 2.0);
            painter.setPen(m_pen);
        }
        painter.restore();
    }
}

void MeasureLineWidthSA::compute(void)
{
    computeLengthSlope();
    double cos_theta = - m_sin_theta; // 与主直线垂直
    double sin_theta = m_cos_theta;

    //视觉寻边之前，设置平行线的默认位置
    m_keyPoints_pix[2].setX( m_keyPoints_pix[0].x() - m_results_pix[0] / 2 * cos_theta );
    m_keyPoints_pix[2].setY( m_keyPoints_pix[0].y() - m_results_pix[0] / 2 * sin_theta );

    m_keyPoints_pix[3].setX( m_keyPoints_pix[1].x() - m_results_pix[0] / 2 * cos_theta );
    m_keyPoints_pix[3].setY( m_keyPoints_pix[1].y() - m_results_pix[0] / 2 * sin_theta );

    m_keyPoints_pix[4].setX( m_keyPoints_pix[0].x() + m_results_pix[0] / 2 * cos_theta );
    m_keyPoints_pix[4].setY( m_keyPoints_pix[0].y() + m_results_pix[0] / 2 * sin_theta );

    m_keyPoints_pix[5].setX( m_keyPoints_pix[1].x() + m_results_pix[0] / 2 * cos_theta );
    m_keyPoints_pix[5].setY( m_keyPoints_pix[1].y() + m_results_pix[0] / 2 * sin_theta );

    m_keyPoints_um[2].setX( m_keyPoints_um[0].x() - m_results_um[0] / 2 * cos_theta );
    m_keyPoints_um[2].setY( m_keyPoints_um[0].y() - m_results_um[0] / 2 * sin_theta );

    m_keyPoints_um[3].setX( m_keyPoints_um[1].x() - m_results_um[0] / 2 * cos_theta );
    m_keyPoints_um[3].setY( m_keyPoints_um[1].y() - m_results_um[0] / 2 * sin_theta );

    m_keyPoints_um[4].setX( m_keyPoints_um[0].x() + m_results_um[0] / 2 * cos_theta );
    m_keyPoints_um[4].setY( m_keyPoints_um[0].y() + m_results_um[0] / 2 * sin_theta );

    m_keyPoints_um[5].setX( m_keyPoints_um[1].x() + m_results_um[0] / 2 * cos_theta );
    m_keyPoints_um[5].setY( m_keyPoints_um[1].y() + m_results_um[0] / 2 * sin_theta );
}

}
